package com.yuerenjf.yuerenjinfu.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yuerenjf.yuerenjinfu.R;
import com.yuerenjf.yuerenjinfu.bean.LendItemBean;
import com.yuerenjf.yuerenjinfu.listener.OnRecyclerViewItemClickListener;

import java.util.ArrayList;


/**
 * Created by fengjh on 16/7/19.
 */
public class AdapterLendZhaiQuanRecycler extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_ITEM = 2;
    private static final int TYPE_EMPTY = 3;
    private View mEmptyView;

    private Context mContext;
    private ArrayList<LendItemBean> mLendItems;
    private OnRecyclerViewItemClickListener mOnRecyclerViewItemClickListener;

    public OnRecyclerViewNoDataClickListener getmOnRecyclerViewNoDataClickListener() {
        return mOnRecyclerViewNoDataClickListener;
    }

    public void setmOnRecyclerViewNoDataClickListener(OnRecyclerViewNoDataClickListener mOnRecyclerViewNoDataClickListener) {
        this.mOnRecyclerViewNoDataClickListener = mOnRecyclerViewNoDataClickListener;
    }

    private OnRecyclerViewNoDataClickListener mOnRecyclerViewNoDataClickListener;

    public AdapterLendZhaiQuanRecycler(Context mContext, ArrayList<LendItemBean> mVideoItems) {
        this.mContext = mContext;
        this.mLendItems = mVideoItems;
    }

    @Override
    public int getItemViewType(int position) {
        int size = mLendItems.size();
        if (size == 0 && null != mEmptyView) {
            return TYPE_EMPTY;
        }
        return TYPE_ITEM;
    }


    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        mOnRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_lend_zhaiquan, parent, false);
            return new VideoViewHolder(inflate);
        } else {

            View inflate = mEmptyView;
            EmptyHolder holder= new EmptyHolder(inflate);
            if(inflate!=null){
                RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);
//                RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams) holder.noDataLayout.getLayoutParams();
//                linearParams.height = RelativeLayout.LayoutParams.MATCH_PARENT;
//                linearParams.width = RelativeLayout.LayoutParams.MATCH_PARENT;
                holder.noDataLayout.setLayoutParams(params);
//                holder.noDataLayout.setLayoutParams(linearParams, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
            }

            return holder;
        }


//
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof VideoViewHolder) {
            LendItemBean bean=mLendItems.get(position);
            VideoViewHolder itemHolder= (VideoViewHolder) holder;

//            itemHolder.mItem_lend_xinshou_status.setText();//新手专享

//            itemHolder.mItem_lend_title.setText(bean.getBorrow_title());

//            itemHolder.mItem_lend_days_text.setText(bean.);


        } else if (holder instanceof EmptyHolder) {
            final EmptyHolder videoViewHolder = (EmptyHolder) holder;
            videoViewHolder.nodata.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("click", "click");
                    mOnRecyclerViewNoDataClickListener.onRecyclerViewNoDataClick();
//                    ToastHelper.makeText(mContext, "点击了", Toast.LENGTH_LONG).setAnimation(R.style.Animation_Toast).show();
                }
            });
        }


    }

    public void setEmptyView(View emptyView) {
        mEmptyView = emptyView;
        notifyItemInserted(0);
    }

    public void removeEmptyView() {
        if(mEmptyView!=null){
            notifyItemRemoved(0);
            mEmptyView = null;
        }

    }

    @Override
    public int getItemCount() {
        int count = 0;
        int size = mLendItems.size();
        if (size == 0 && null != mEmptyView) {
            count = 1;
        }else{
            count=size;
        }
        return count;
    }

    public class VideoViewHolder extends RecyclerView.ViewHolder {
        private TextView mItem_lend_diya_status;
        private TextView mItem_lend_xinshou_status;
        private TextView mItem_lend_title;
        private TextView mItem_lend_shouyilv_top;
        private TextView mTv_home_income;
        private TextView mLend_youxuan_percent_other;
        private TextView mItem_lend_days_text;
        private TextView mItem_lend_total_text;
        private TextView mItem_lend_total_unit_text;
        private TextView mItem_lend_youxuan_last_money_text;
        private TextView mTv_newlevel_text;
        private ProgressBar mPb_newlevel;
        private TextView mItem_lend_youxuan_sell_status_text;



        public VideoViewHolder(View itemView) {
            super(itemView);

                mItem_lend_diya_status = (TextView) itemView.findViewById(R.id.item_lend_diya_status);
                mItem_lend_xinshou_status = (TextView) itemView.findViewById(R.id.item_lend_xinshou_status);
                mItem_lend_title = (TextView) itemView.findViewById(R.id.item_lend_title);
                mItem_lend_shouyilv_top = (TextView) itemView.findViewById(R.id.item_lend_shouyilv_top);
                mTv_home_income = (TextView) itemView.findViewById(R.id.tv_home_income);
                mLend_youxuan_percent_other = (TextView) itemView.findViewById(R.id.lend_youxuan_percent_other);
                mItem_lend_days_text = (TextView)itemView. findViewById(R.id.item_lend_days_text);
                mItem_lend_total_text = (TextView) itemView.findViewById(R.id.item_lend_total_text);
                mItem_lend_total_unit_text = (TextView) itemView.findViewById(R.id.item_lend_total_unit_text);
                mItem_lend_youxuan_last_money_text = (TextView) itemView.findViewById(R.id.item_lend_youxuan_last_money_text);
                mTv_newlevel_text = (TextView) itemView.findViewById(R.id.tv_newlevel_text);
                mPb_newlevel = (ProgressBar) itemView.findViewById(R.id.pb_newlevel);
                mItem_lend_youxuan_sell_status_text = (TextView) itemView.findViewById(R.id.item_lend_youxuan_sell_status_text);


        }
    }

    class EmptyHolder extends RecyclerView.ViewHolder {
        TextView nodata;

        RelativeLayout noDataLayout;
        public EmptyHolder(View itemView) {
            super(itemView);
        }
    }


    public interface OnRecyclerViewNoDataClickListener {
        void onRecyclerViewNoDataClick();
    }
}
